home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cserial.zip / MDM_INIT.C < prev    next >
C/C++ Source or Header  |  1990-04-04  |  5KB  |  179 lines

  1. /*
  2.  *                              MDM_INIT.C
  3.  *
  4.  *                           Written for the
  5.  *
  6.  *                              Datalight
  7.  *                           Microsoft V 5.x
  8.  *                                TurboC
  9.  *                                  &
  10.  *                               Zortech
  11.  *
  12.  *                             C Compilers
  13.  *
  14.  *            Copyright (c) John Birchfield 1987, 1988, 1989
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include "8250xon.h"
  20. #include "ctrl_brk.h"
  21. #include "timer.h"
  22.  
  23. #if (!defined (TRUE))
  24. #    define TRUE (1)
  25. #    define FALSE (0)
  26. #endif
  27.  
  28. #define ETX 3
  29. #define EOT 4
  30. void usage (void);
  31.  
  32. main (int argc, char **argv)
  33. {
  34.     char    m_cmd[80];
  35.     int     port = 1, baud = 0, data = 0, stop = 0, parity = 0,
  36.             p_init = FALSE;
  37.     while (argc > 1)
  38.     {
  39.         if (*argv [1] == '?')
  40.             usage ();
  41.         if (argv [1] [0] != '-')
  42.         {
  43.             break;
  44.         }
  45.         argc--; argv++;
  46.         switch (toupper (argv[0][1]))
  47.         {
  48.             case 'B':
  49.                 baud = atoi (&argv[0][2]);
  50.                 break;
  51.             case 'C':
  52.                 port = atoi (&argv[0][2]);
  53.                 break;
  54.             case 'P':
  55.                 parity = argv[0][2];
  56.                 break;
  57.             case 'S':
  58.                 stop = atoi (&argv[0][2]);
  59.                 break;
  60.             case 'D':
  61.                 data = atoi (&argv[0][2]);
  62.                 break;
  63.             default:
  64.                 fprintf (stderr, "Invalid option %s\n", *argv);
  65.                 usage ();
  66.                 break;
  67.         }
  68.     }
  69.  
  70.  
  71.     if (baud || data || parity || stop)
  72.     {
  73.         baud = (baud) ? baud : 1200;
  74.         data = (data) ? data : 8;
  75.         parity = (parity) ? toupper (parity) : (int) 'N';
  76.         stop = (stop) ? stop : 1;
  77.         sprintf (m_cmd, "%d %c %d %d", baud, parity, stop, data);
  78.         p_init = TRUE;
  79.     }
  80.     trap_ctrl_break ();
  81.     timer_init ();
  82.     xon8250_init (port, 256);
  83.     if (p_init)
  84.         xon8250_port_init (m_cmd);
  85.     else
  86.         xon8250_port_enable ();
  87.     modem_write ("#+++");
  88.     timeout (1);
  89.     while (argc-- > 1)
  90.     {
  91.         modem_write ("AT");
  92.         modem_write (*++argv);
  93.         modem_write ("\r");
  94.     }
  95.     while (!xon8250_write_buffer_empty ())
  96.         ;
  97.     timeout (1);
  98.     xon8250_term (0);
  99.     timer_term ();
  100.     release_ctrl_break ();
  101.     exit (0);
  102. }
  103.  
  104.  
  105.  
  106.  
  107. int
  108. modem_write (char *s)
  109. {
  110.     char    ch, *cp;
  111.     for (cp = s; *cp; cp++)
  112.     {
  113.         switch (*cp)
  114.         {
  115.             case '~':
  116.                 timeout (1);
  117.                 continue;
  118.             case '#':
  119.                 xon8250_dtnr ();
  120.                 continue;
  121.             case '^':
  122.                 ch = *++cp;
  123.                 ch = toupper (ch) - '@';
  124.                 while (xon8250_write (ch) == -1)
  125.                     ;
  126.                 break;
  127.             case '\\':
  128.                 switch (ch = *++cp)
  129.                 {
  130.                     case 'e':
  131.                         ch = 0x1b;
  132.                         break;
  133.                     case 'r':
  134.                         ch = '\r';
  135.                         break;
  136.                     case 'n':
  137.                         ch = '\n';
  138.                         break;
  139.                     case 'b':
  140.                         ch = '\b';
  141.                         break;
  142.                     case 'a':
  143.                         ch = '\a';
  144.                         break;
  145.                     case 'f':
  146.                         ch = '\f';
  147.                         break;
  148.                     default:
  149.                         break;
  150.                 }
  151.                 while (xon8250_write (ch) == -1)
  152.                     ;
  153.                 break;
  154.             default:
  155.                 while (xon8250_write (*cp) == -1)
  156.                     ;
  157.                 break;
  158.         }
  159.     }
  160. }
  161.  
  162. void
  163. usage (void)
  164. {
  165.     fputs ("MDM_INIT:\n", stderr);
  166.     fputs ("Use the following command line options.\n\n", stderr);
  167.     fputs ("   -C[1 or 2] Com Port 1 or 2      {Default Com Port 1}\n", stderr);
  168.     fputs ("   -B[300 600 1200 2400 4800 9600] {Default 9600 Baud}\n", stderr);
  169.     fputs ("   -D[7 or 8]  Data Bits           {Default 8}\n", stderr);
  170.     fputs ("   -S[1 or 2] Stop Bits            {Default 1}\n", stderr);
  171.     fputs ("   -P[E O or N] Parity             {Default None}\n\n", stderr);
  172.     fputs ("   -b2400 -pn -s1 -d8 -c1\n\n", stderr);
  173.     fputs ("If any port options are set then the port will be left\n", stderr);
  174.     fputs ("in that state when the program exits, otherwise we just\n", stderr);
  175.     fputs (" enable the port and blast the strings we are passed to it\n\n", stderr);
  176.     exit (1);
  177. }
  178.  
  179.